#include "ASFilledArea.h"
#include "..\base\ASGraphic.h"

static as_ret as_ctrl_filledarea_set_shape(as_control* ctrl_p, as_filled_area_shape shape)
{
    as_filled_area* fa_p = (as_filled_area*)ctrl_p;

    if(!fa_p || fa_p->shape == shape)
        return AS_INVALID_PARAM;

    fa_p->shape = shape;
    return AS_OK;
}
static as_ret as_ctrl_filledarea_set_fill_color(as_control* ctrl_p, ASUINT color)
{
    as_filled_area* fa_p = (as_filled_area*)ctrl_p;

    if(!fa_p || fa_p->fill_color == color)
        return AS_INVALID_PARAM;

    fa_p->fill_color = color;
    return AS_OK;
}
static as_ret as_ctrl_filledarea_set_frame_color(as_control* ctrl_p, ASUINT color)
{
    as_filled_area* fa_p = (as_filled_area*)ctrl_p;

    if(!fa_p || fa_p->frame_color == color)
        return AS_INVALID_PARAM;

    fa_p->frame_color = color;
    return AS_OK;
}

static as_ret as_ctrl_filledarea_set_round_width(as_control* ctrl_p, ASINT width)
{
    as_filled_area* fa_p = (as_filled_area*)ctrl_p;

    if(!fa_p || fa_p->shape != AS_FA_SHAPE_ROUNDRECT || fa_p->round_width == width)
        return AS_INVALID_PARAM;

    fa_p->round_width = width;
    return AS_OK;
}

static void as_ctrl_filledarea_paint(as_control* ctrl_p)
{
    as_filled_area* fa_p = (as_filled_area*)ctrl_p;
    as_rect* rect_p;
    ASINT layer;
    ASUINT8* paint_buf;

    if(!fa_p || ctrl_p->if_hidden)
        return;

    layer = ctrl_p->show_hdlr;

    if(layer < 0)
        return;

    rect_p = &fa_p->base.area;
    as_set_layer_clip_ex(layer, rect_p);
    paint_buf = as_get_layer_buffer_by_hdlr(layer);

    if(fa_p->shape == AS_FA_SHAPE_RECT)
    {
        as_fill_rect( rect_p, fa_p->frame_color, fa_p->fill_color);
    }
    else if(fa_p->shape == AS_FA_SHAPE_ROUNDRECT)
    {
        as_fill_rect(rect_p, fa_p->frame_color, fa_p->fill_color);
        //if(fa_p->round_width <= 0)
        //    fa_p->round_width = 3;
        //vm_graphic_fill_roundrect(fa_p->base.paint_buf, rect_p->x, rect_p->y, rect_p->w, rect_p->h,
        //    fa_p->round_width, fill_color);
        //vm_graphic_roundrect(fa_p->base.paint_buf, rect_p->x, rect_p->y, rect_p->w, rect_p->h,
        //    fa_p->round_width, frame_color);
    }

    as_reset_layer_clip(layer);
}

as_filled_area* as_ctrl_filledarea_create(as_rect area)
{
    as_filled_area* fa_p = NULL;
    fa_p = as_calloc(sizeof(as_filled_area));

    if(!fa_p)
        return NULL;

    as_ctrl_init((as_control*)fa_p, area);
    fa_p->base.paint_hdlr = as_ctrl_filledarea_paint;
    fa_p->base.ctrl_type = AS_CTRL_FILLEDAREA;
    fa_p->set_shape = as_ctrl_filledarea_set_shape;
    fa_p->set_fill_color = as_ctrl_filledarea_set_fill_color;
    fa_p->set_frame_color = as_ctrl_filledarea_set_frame_color;
    fa_p->set_round_width = as_ctrl_filledarea_set_round_width;
    fa_p->fill_color = 0x0;
    fa_p->frame_color = 0x0;
    fa_p->shape = AS_FA_SHAPE_RECT;
    return fa_p;
}

as_ret as_ctrl_filledarea_destroy(as_filled_area* ctrl_filledarea_p)
{
    if(!ctrl_filledarea_p)
        return AS_INVALID_PARAM;

    as_free(ctrl_filledarea_p);
    ctrl_filledarea_p = NULL;
    return AS_OK;
}